﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Web;
using System.Drawing;

namespace INSTA.MusiqueStore.Library
{
    /// <summary>
    /// Class Convert.
    /// </summary>
    public class Tips
    {
       

        /// <summary>
        /// Converts to a human readable time.
        /// </summary>
        /// <param name="seconds">The time in seconds.</param>
        /// <returns>The converted time.</returns>
        public static string ToDisplayableTime(int seconds)
        {
            string res = "";

            if (seconds < 60)
            {
                res = "00:" + AddZeroToTime(seconds.ToString());
            }
            else if (seconds < 60 * 60)
            {
                var s = (seconds % 60).ToString();
                var m = ((int)(seconds / 60)).ToString();
                res = AddZeroToTime(m) + ":" + AddZeroToTime(s);
            }
            else
            {
                var s = (seconds % (60)).ToString();
                var m = ((int)((seconds % (60 * 60)) / 60)).ToString();
                var h = ((int)(seconds / (60 * 60))).ToString();

                res = AddZeroToTime(h) + ":" + AddZeroToTime(m) + ":" + AddZeroToTime(s);
            }
            return res;
        }


        /// <summary>
        /// Adds the zero to time.
        /// </summary>
        /// <param name="time">The time.</param>
        /// <returns>The formatted time.</returns>
        public static string AddZeroToTime(string time)
        {
            var res = time;

            if (time.Length < 2)
            {
                res = "0" + time;
            }
            return res;
        }


        /// <summary>
        /// Downloads the specified urlFichier.
        /// </summary>
        /// <param name="h">The HTTP context.</param>
        /// <param name="urlFichier">The file URL.</param>
        public static void Download(HttpContext h, string urlFichier)
        {
            // On récupère les informations sur le fichier
            var file = new FileInfo(urlFichier);
            // On efface le response
            h.Response.Clear();
            // On ajoute les entêtes pour le fichier
            h.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            h.Response.AddHeader("Content-Length", file.Length.ToString());
            h.Response.ContentType = "application/octet-stream";
            // On lance le téléchargement
            h.Response.WriteFile(file.FullName);
            // On ferme le response
            h.Response.Flush();
            h.Response.Close();
        }

       
        /// <summary>
        /// Reads the infos.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The title.</returns>
        public static TagLib.File ReadInfos(string path)
        {
            var f = TagLib.File.Create(path);
            return f;
        }

    }
}
